#!/usr/bin/env node

var fs = require("fs");
var path = require("path");
var program = require("commander");

program.version('0.1.5')
    .option('-i, --inputfile [value]', "input markdown file")
    .option('-o, --outputfile [value]', "the result markdown file")
    .parse(process.argv);


var inputFile = program.args[0];
if (program.inputfile) {
    inputFile = program.inputfile;
}
inputFile = path.resolve(process.cwd(), inputFile);
var istat = fs.statSync(inputFile);
if (!istat) {
    console.log("未找到输入文件，转换失败");
    process.exit(0);
}
var outputFile = program.args[1] || inputFile + ".space";
if (program.outputfile) {
    outputFile = program.outputfile;
}
outputFile = path.resolve(process.cwd(), outputFile);

fs.readFile(inputFile, function (err, content) {
    if (err) {
        console.log(err);
        return;
    }
    var result = content.toString().replace(/\t/g, "    ");

    fs.writeFile(outputFile, result, function (err) {
        console.log(err || "completed");
    });
});
